package com.mk.settlement.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DuplicateKeyException;

import com.mk.settlement.mappers.SettlementRelationMapper;
import com.mk.settlement.mappers.SettlementRuleMapper;
import com.mk.settlement.model.SettlementRelation;
import com.mk.settlement.model.SettlementRule;
import com.mk.settlement.service.ISettlementRelationService;


public class SettlementRelationService implements ISettlementRelationService {

    private Logger log = LoggerFactory.getLogger(SettlementRelationService.class);
    
    
    @Resource
    private SettlementRelationMapper settlementRelationMapper;
    
    @Resource
    private SettlementRuleMapper settlementRuleMapper;

    @Override
    public boolean addSettlementRelation(SettlementRelation settlementRelation) {

        
        log.info("添加酒店-规则对应关系...");
        
        if(settlementRelation != null) {
            
            log.info("输入参数:" + settlementRelation.toString());
            
            if(settlementRelation.getSettlementThreshold() != null) {
                settlementRelation.getSettlementThreshold().setScale(2);
            }
            
            try {
                int iResult = settlementRelationMapper.insert(settlementRelation);
                if (iResult > 0) {
                    
                    log.info("添加酒店-规则对应关系成功.");
                    
                    return true;
                } else {
                    log.info("添加酒店-规则对应关系失败!");
                    
                    return false;
                }
            } catch (DuplicateKeyException e) {
                log.error("酒店id:" + settlementRelation.getSettlementTargetId() + "已经存在规则settlementRuleId为" + settlementRelation.getSettlementRuleId() + "的对应关系.");
                return false;
            } catch (Exception e) {
                log.error("插入酒店规则对应关系异常!", e);
                
                return false;
            }
        } else {
            
            log.info("传入参数为空!");
            
            return false;
        }
        
    }
    
    @Override
    public List<SettlementRule> getAllRule() {
        
        return settlementRuleMapper.getAllRule();
        
    }
    
}
